Skip to content

Conversation

@tangaac
Copy link
Member

@tangaac tangaac commented May 9, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/pr-subscribers-backend-loongarch

Author: None (tangaac)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/139201.diff

2 Files Affected:

  • (modified) llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td (+8)
  • (modified) llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td (+21)
diff --git a/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td
index fe08c1050b4d7..802fd082564e1 100644
--- a/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td
+++ b/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td
@@ -1756,6 +1756,14 @@ def : Pat<(lasxsplatf32 FPR32:$fj),
 def : Pat<(lasxsplatf64 FPR64:$fj),
           (XVREPLVE0_D (SUBREG_TO_REG (i64 0), FPR64:$fj, sub_64))>;
 
+// VSTELM
+defm : VstelmPat<truncstorei8, v32i8, XVSTELM_B, simm12_addlike, uimm5>;
+defm : VstelmPat<truncstorei16, v16i16, XVSTELM_H, simm11_lsl1, uimm4>;
+defm : VstelmPat<truncstorei32, v8i32, XVSTELM_W, simm10_lsl2, uimm3>;
+defm : VstelmPat<store, v4i64, XVSTELM_D, simm9_lsl3, uimm2>;
+defm : VstelmPat<store, v8f32, XVSTELM_W, simm10_lsl2, uimm3, f32>;
+defm : VstelmPat<store, v4f64, XVSTELM_D, simm9_lsl3, uimm2, f64>;
+
 // Loads/Stores
 foreach vt = [v32i8, v16i16, v8i32, v4i64, v8f32, v4f64] in {
   defm : LdPat<load, XVLD, vt>;
diff --git a/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td
index 1ffc5f8056b96..69fbf5ae45603 100644
--- a/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td
+++ b/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td
@@ -1446,6 +1446,20 @@ multiclass VldreplPat<ValueType vt, LAInst Inst, Operand ImmOpnd> {
             (Inst BaseAddr:$rj, ImmOpnd:$imm)>;
 }
 
+multiclass VstelmPat<PatFrag StoreOp, ValueType vt, LAInst Inst,
+                     Operand ImmOpnd, Operand IdxOpnd, ValueType elt = i64> {
+  def : Pat<(StoreOp(elt(vector_extract vt:$vd, IdxOpnd:$idx)), BaseAddr:$rj),
+            (Inst vt:$vd, BaseAddr:$rj, 0, IdxOpnd:$idx)>;
+
+  def : Pat<(StoreOp(elt(vector_extract vt:$vd, IdxOpnd:$idx)),
+                (AddrConstant GPR:$rj, ImmOpnd:$imm)),
+            (Inst vt:$vd, GPR:$rj, ImmOpnd:$imm, IdxOpnd:$idx)>;
+
+  def : Pat<(StoreOp(elt(vector_extract vt:$vd, IdxOpnd:$idx)),
+                (AddLike BaseAddr:$rj, ImmOpnd:$imm)),
+            (Inst vt:$vd, BaseAddr:$rj, ImmOpnd:$imm, IdxOpnd:$idx)>;
+}
+
 let Predicates = [HasExtLSX] in {
 
 // VADD_{B/H/W/D}
@@ -1935,6 +1949,13 @@ def : Pat<(lsxsplatf32 FPR32:$fj),
 def : Pat<(lsxsplatf64 FPR64:$fj),
           (VREPLVEI_D (SUBREG_TO_REG (i64 0), FPR64:$fj, sub_64), 0)>;
 
+defm : VstelmPat<truncstorei8, v16i8, VSTELM_B, simm12_addlike, uimm4>;
+defm : VstelmPat<truncstorei16, v8i16, VSTELM_H, simm11_lsl1, uimm3>;
+defm : VstelmPat<truncstorei32, v4i32, VSTELM_W, simm10_lsl2, uimm2>;
+defm : VstelmPat<store, v2i64, VSTELM_D, simm9_lsl3, uimm1>;
+defm : VstelmPat<store, v4f32, VSTELM_W, simm10_lsl2, uimm2, f32>;
+defm : VstelmPat<store, v2f64, VSTELM_D, simm9_lsl3, uimm1, f64>;
+
 // Loads/Stores
 foreach vt = [v16i8, v8i16, v4i32, v2i64, v4f32, v2f64] in {
   defm : LdPat<load, VLD, vt>;

@tangaac
Copy link
Member Author

tangaac commented May 9, 2025

Files optimized.
lsx: tangaac/loong-opt-cov-ts@758432b
lasx: tangaac/loong-opt-cov-ts@65077c5

@tangaac tangaac requested review from SixWeining, heiher and wangleiat May 9, 2025 06:20
@tangaac tangaac requested a review from heiher May 16, 2025 03:02
Copy link
Member

@heiher heiher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits.

@tangaac tangaac merged commit f87bcf1 into llvm:main May 21, 2025
11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 21, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-gcc-ubuntu running on sie-linux-worker3 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/174/builds/18095

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'AddressSanitizer-x86_64-linux-dynamic :: TestCases/asan_lsan_deadlock.cpp' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/./bin/clang  --driver-mode=g++ -fsanitize=address -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -gline-tables-only  -m64  -shared-libasan -O0 /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp -o /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxDynamicConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp # RUN: at line 4
+ /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/./bin/clang --driver-mode=g++ -fsanitize=address -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -gline-tables-only -m64 -shared-libasan -O0 /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp -o /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxDynamicConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp
env ASAN_OPTIONS=detect_leaks=1 not  /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxDynamicConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp 2>&1 | FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp # RUN: at line 5
+ FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp
+ env ASAN_OPTIONS=detect_leaks=1 not /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxDynamicConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp
�[1m/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp:58:12: �[0m�[0;1;31merror: �[0m�[1mCHECK: expected string not found in input
�[0m // CHECK: SUMMARY: AddressSanitizer: stack-buffer-overflow
�[0;1;32m           ^
�[0m�[1m<stdin>:1:1: �[0m�[0;1;30mnote: �[0m�[1mscanning from here
�[0m=================================================================
�[0;1;32m^
�[0m�[1m<stdin>:2:10: �[0m�[0;1;30mnote: �[0m�[1mpossible intended match here
�[0m==1838357==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7b59ff1de034 at pc 0x557c97cfc220 bp 0x7b59fd3fdce0 sp 0x7b59fd3fdcd8
�[0;1;32m         ^
�[0m
Input file: <stdin>
Check file: /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
�[1m�[0m�[0;1;30m            1: �[0m�[1m�[0;1;46m================================================================= �[0m
�[0;1;31mcheck:58'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
�[0m�[0;1;30m            2: �[0m�[1m�[0;1;46m==1838357==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7b59ff1de034 at pc 0x557c97cfc220 bp 0x7b59fd3fdce0 sp 0x7b59fd3fdcd8 �[0m
�[0;1;31mcheck:58'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;35mcheck:58'1              ?                                                                                                                                    possible intended match
�[0m�[0;1;30m            3: �[0m�[1m�[0;1;46mWRITE of size 4 at 0x7b59ff1de034 thread T2 �[0m
�[0;1;31mcheck:58'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m>>>>>>

--

********************


@SixWeining
Copy link
Contributor

This change causes SPEC2017 521.wrf_r fails to build with -O3 -mlasx:

LLVM ERROR: Error while trying to spill R6 from class GPR: Cannot scavenge register without an emergency spill slot!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang -fc1 -triple loongarch64-unknown-linux-gnu -emit-obj -I /home/llvm-ci/upstream/llvm-test-suite-139201/_build/External/SPEC/CFP2017rate/521.wrf_r -I /home/llvm-ci/upstream/llvm-test-suite-139201/External/SPEC/cpu2017-v1.1.5/benchspec/CPU/521.wrf_r/src -I /home/llvm-ci/upstream/llvm-test-suite-139201/External/SPEC/cpu2017-v1.1.5/benchspec/CPU/521.wrf_r/src/netcdf/include -I /home/llvm-ci/upstream/llvm-test-suite-139201/External/SPEC/cpu2017-v1.1.5/benchspec/CPU/521.wrf_r/src/inc -ffixed-line-length=72 -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu loongarch64 -target-feature +64bit -target-feature +f -target-feature +d -target-feature +ual -target-feature +lsx -target-feature +lasx -vectorize-loops -vectorize-slp -fversion-loops-for-stride -module-dir External/SPEC/CFP2017rate/521.wrf_r -resource-dir /home/llvm-ci/upstream/llvm-project-139201/_build/lib/clang/21 -mframe-pointer=none -O3 -o External/SPEC/CFP2017rate/521.wrf_r/CMakeFiles/wrf_specpp_rate.dir/module_ra_rrtm.F90.o -x f95 External/SPEC/CFP2017rate/521.wrf_r/CMakeFiles/wrf_specpp_rate.dir/module_ra_rrtm.F90-pp.f90
1.	Running pass 'Function Pass Manager' on module 'FIRModule'.
2.	Running pass 'Prologue/Epilogue Insertion & Frame Finalization' on function '@_QMmodule_ra_rrtmPtaugb12'
 #0 0x0000555559ea6ef0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1d06ef0)
 #1 0x0000555559ea4bbc llvm::sys::RunSignalHandlers() (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1d04bbc)
 #2 0x0000555559ea7a7c SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x00007ffffc03cf94 (linux-vdso.so.1+0xf94)
 #4 0x00002aaaad5d8440 __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00002aaaad583f7c raise ./signal/../sysdeps/posix/raise.c:27:6
 #6 0x00002aaaad56d2e4 abort ./stdlib/abort.c:81:3
 #7 0x0000555559e46528 llvm::report_fatal_error(llvm::Twine const&, bool) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1ca6528)
 #8 0x000055555c945180 llvm::RegScavenger::scavengeRegisterBackwards(llvm::TargetRegisterClass const&, llvm::MachineInstrBundleIterator<llvm::MachineInstr, false>, bool, int, bool) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x47a5180)
 #9 0x000055555c945708 llvm::RegScavenger::scavengeRegisterBackwards(llvm::TargetRegisterClass const&, llvm::MachineInstrBundleIterator<llvm::MachineInstr, false>, bool, int, bool) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x47a5708)
#10 0x000055555c9460a4 scavengeVReg(llvm::MachineRegisterInfo&, llvm::RegScavenger&, llvm::Register, bool) RegisterScavenging.cpp:0:0
#11 0x000055555c945c24 scavengeFrameVirtualRegsInBlock(llvm::MachineRegisterInfo&, llvm::RegScavenger&, llvm::MachineBasicBlock&) RegisterScavenging.cpp:0:0
#12 0x000055555c945888 llvm::scavengeFrameVirtualRegs(llvm::MachineFunction&, llvm::RegScavenger&) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x47a5888)
#13 0x000055555c8e8904 (anonymous namespace)::PEIImpl::run(llvm::MachineFunction&) PrologEpilogInserter.cpp:0:0
#14 0x000055555c8e9230 (anonymous namespace)::PEILegacy::runOnMachineFunction(llvm::MachineFunction&) PrologEpilogInserter.cpp:0:0
#15 0x000055555c7fdc60 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x465dc60)
#16 0x000055555ecfc670 llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x6b5c670)
#17 0x000055555ed03b04 llvm::FPPassManager::runOnModule(llvm::Module&) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x6b63b04)
#18 0x000055555ecfd214 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x6b5d214)
#19 0x0000555559eeb7e0 generateMachineCodeOrAssemblyImpl(clang::DiagnosticsEngine&, llvm::TargetMachine&, Fortran::frontend::BackendActionTy, llvm::Module&, Fortran::frontend::CodeGenOptions const&, llvm::raw_pwrite_stream&) FrontendActions.cpp:0:0
#20 0x0000555559eeb4e0 Fortran::frontend::CodeGenAction::executeAction() (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1d4b4e0)
#21 0x0000555559edd7e0 Fortran::frontend::FrontendAction::execute() (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1d3d7e0)
#22 0x0000555559ebe140 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1d1e140)
#23 0x0000555559ee176c Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1d4176c)
#24 0x0000555559cfa814 fc1_main(llvm::ArrayRef<char const*>, char const*) (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1b5a814)
#25 0x0000555559cf9afc main (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1b59afc)
#26 0x00002aaaad56d8b0 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#27 0x00002aaaad56d998 call_init ./csu/../csu/libc-start.c:128:20
#28 0x00002aaaad56d998 __libc_start_main ./csu/../csu/libc-start.c:347:5
#29 0x0000555559cf89c8 L0� (/home/llvm-ci/upstream/llvm-project-139201/_build/bin/flang+0x1b589c8)
flang-21: error: unable to execute command: Aborted
flang-21: error: flang frontend command failed due to signal (use -v to see invocation)
flang version 21.0.0git
Target: loongarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/llvm-ci/upstream/llvm-project-139201/_build/bin
flang-21: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-21: note: diagnostic msg: /tmp/module_ra_rrtm-ddc6a8
flang-21: note: diagnostic msg: /tmp/module_ra_rrtm-ddc6a8.sh
flang-21: note: diagnostic msg: 

********************

A reduced llvm ir is attached.
reduced.ll.txt

tangaac added a commit that referenced this pull request Jul 18, 2025
…146455)

This patch adds an emergency spill slot when ran out of registers.
PR #139201 introduces `vstelm` instructions with only 8-bit imm offset, 
it causes no spill slot to store the spill registers.
@tangaac tangaac deleted the vstelm branch July 18, 2025 09:09
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 24, 2025
…lvm#146455)

This patch adds an emergency spill slot when ran out of registers.
PR llvm#139201 introduces `vstelm` instructions with only 8-bit imm offset,
it causes no spill slot to store the spill registers.

(cherry picked from commit 64a0478)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants